///////////////////////////////////////////////////////////////////////////
// C++ code generated with wxFormBuilder (version Jun 17 2015)
// http://www.wxformbuilder.org/
//
// PLEASE DO "NOT" EDIT THIS FILE!
///////////////////////////////////////////////////////////////////////////
#include <wx/msgdlg.h>
#include <memory.h>

#include "MainFrame.h"
#include "ToolBar.h"
#include "DataType.h"
#include "AboutDialog.h"

BEGIN_EVENT_TABLE(MainFrame, wxFrame )
	EVT_TOOL(TEXT_TOOL_TOOL_ID_NEW_FILE, MainFrame::_wxEvent_CreateNew)
	EVT_TOOL(TEXT_TOOL_TOOL_ID_OPEN_FILE, MainFrame::_wxEvent_OpenFile)
	EVT_TOOL(TEXT_TOOL_TOOL_ID_SAVE_FILE, MainFrame::_wxEvent_SaveFile)
	EVT_TOOL(TEXT_TOOL_TOOL_ID_RECODE, MainFrame::_wxEvent_GenerateCode)
	EVT_TOOL(TEXT_TOOL_TOOL_ID_ABOUT, MainFrame::_wxEvent_OnAbout)
	EVT_TEXT(wxID_ANY, MainFrame::_wxEvent_OnTextChanged)
END_EVENT_TABLE()
///////////////////////////////////////////////////////////////////////////
MainFrame::MainFrame(wxWindow* pclsParent, wxWindowID iId) :
wxFrame(pclsParent, iId, _I18N(APP_NAME), wxDefaultPosition, wxSize(640, 480), wxDEFAULT_FRAME_STYLE|wxTAB_TRAVERSAL)
{
	SetSizeHints( wxDefaultSize, wxDefaultSize );

	m_pclsToolBar_ToolBar = CreateTextInputToolBar(this);

	wxBoxSizer* pclsBoxSizer_RootSizer = new wxBoxSizer( wxVERTICAL );

	m_pclsNotebook_NoteBook = new wxNotebook( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, 0 );

	m_pclsTextCtrl_TextEdit = new wxTextCtrl(m_pclsNotebook_NoteBook, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
	m_pclsTextCtrl_FontLib = new wxTextCtrl(m_pclsNotebook_NoteBook, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );
	m_pclsTextCtrl_EncodeString = new wxTextCtrl(m_pclsNotebook_NoteBook, wxID_ANY, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_MULTILINE );

	m_pclsNotebook_NoteBook->AddPage(m_pclsTextCtrl_TextEdit, _I18N("Source Text"), true);
	m_pclsNotebook_NoteBook->AddPage(m_pclsTextCtrl_FontLib, _I18N("Extracted Text"));
	m_pclsNotebook_NoteBook->AddPage(m_pclsTextCtrl_EncodeString, _I18N("Recode text"));

	pclsBoxSizer_RootSizer->Add(m_pclsNotebook_NoteBook, 1, wxEXPAND, 5 );

	m_pclsStatusBar_StatusBar = this->CreateStatusBar( 1, wxST_SIZEGRIP, wxID_ANY );

	SetSizer( pclsBoxSizer_RootSizer );
	Layout();
	Centre( wxBOTH );

	_initialzie();
}

MainFrame::~MainFrame()
{
}

void MainFrame::_initialzie(void)
{
	m_pclsTextCtrl_TextEdit->Clear();

	m_bTextChanged = false;
	m_strWorkingFileName = wxEmptyString;
	m_pclsStatusBar_StatusBar->SetLabel(m_strWorkingFileName);
}

void MainFrame::CreateNew(wxCommandEvent& clsEvent)
{
	if(true == m_bTextChanged)
	{
		int			iMsgResult;

		iMsgResult = wxMessageBox(_I18N("Content is already changed, Do you want to save your changes?"), _I18N("Save file."), wxYES_NO|wxCANCEL|wxCENTER, this);

		switch(iMsgResult)
		{
			case wxCANCEL:
			{
				break;
			}
			case wxYES:
			{
				if(wxID_CANCEL == _saveFile())
				{
					break;
				}
				// no break;
			}
			case wxNO:
			{
				_initialzie();
				break;
			}
			default:
			{
				break;
			}
		}
	}
	else
	{
		_initialzie();
	}
}

void MainFrame::OpenFile(wxCommandEvent& clsEvent)
{
	if(true == m_bTextChanged)
	{
		int			iMsgResult;

		iMsgResult = wxMessageBox(_I18N("Content is already changed, Do you want to save your changes?"), _I18N("Save file."), wxYES_NO|wxCANCEL|wxCENTER, this);

		switch(iMsgResult)
		{
			case wxCANCEL:
			{
				break;
			}
			case wxYES:
			{
				if(wxID_CANCEL == _saveFile())
				{
					break;
				}
				// no break;
			}
			case wxNO:
			{
				_openFile();
				m_bTextChanged = false;
				break;
			}
			default:
			{
				break;
			}
		}
	}
	else
	{
		_openFile();
		m_bTextChanged = false;
	}
}

void MainFrame::SaveFile(wxCommandEvent& clsEvent)
{
    _saveFile();
    m_bTextChanged = false;
}

void MainFrame::OnTextChanged(wxCommandEvent& clsEvent)
{
	m_bTextChanged = true;
}

void MainFrame::GenerateCode(wxCommandEvent& clsEvent)
{
	wxString			strContent = m_pclsTextCtrl_TextEdit->GetValue();
	DumpTextData		clsDumpData(strContent);
	bool				bUseFullASCII = false;
	wxToolBarToolBase*	pclsUseFullASCIISwitch = SearchTool(TEXT_TOOL_TOOL_ID_FULL_ASCII);
	if(NULL != pclsUseFullASCIISwitch)
	{
		bUseFullASCII = pclsUseFullASCIISwitch->IsToggled();
	}

	// Text Font Lib.
	m_pclsTextCtrl_FontLib->Clear();
	if((clsDumpData.GetHalfWidthCharacter().Length()>0) && (false == bUseFullASCII))
	{
		m_pclsTextCtrl_FontLib->AppendText(clsDumpData.GetHalfWidthCharacter() + wxT("\n"));
	}
	if(clsDumpData.GetFullWidthCharacter().Length()>0)
	{
		m_pclsTextCtrl_FontLib->AppendText(clsDumpData.GetFullWidthCharacter() + wxT("\n"));
	}
	m_pclsNotebook_NoteBook->SetSelection(1);

	m_pclsTextCtrl_EncodeString->Clear();
	for(int iLine=0; iLine<m_pclsTextCtrl_TextEdit->GetNumberOfLines(); iLine++)
	{
		if(m_pclsTextCtrl_TextEdit->GetLineText(iLine).Length()>0)
		{
			m_pclsTextCtrl_EncodeString->AppendText(clsDumpData.GetTextEncode(m_pclsTextCtrl_TextEdit->GetLineText(iLine), bUseFullASCII));
			m_pclsTextCtrl_EncodeString->AppendText(wxT("\n"));
		}
	}
}

void MainFrame::About(wxCommandEvent& clsEvent)
{
    AboutDialog* pclsAboutDialog = new AboutDialog(this);
    if(NULL != pclsAboutDialog)
	{
		pclsAboutDialog->ShowModal();
	}
}

int MainFrame::_openFile(void)
{
	int			iMsgResult = wxID_CANCEL;

	if(NULL != m_pclsTextCtrl_TextEdit)
	{
		wxFileDialog clsFileDlg(this, _I18N("Open file"), wxEmptyString, wxEmptyString, wxT("Text file(*.txt)|*.txt|All files|(*.*)"));

		iMsgResult = clsFileDlg.ShowModal();

		if(wxID_OK == iMsgResult)
		{
			m_strWorkingFileName = clsFileDlg.GetPath();
			m_pclsTextCtrl_TextEdit->LoadFile(m_strWorkingFileName);
		}

		m_pclsStatusBar_StatusBar->SetLabel(m_strWorkingFileName);
	}

	return iMsgResult;
}

int MainFrame::_saveFile(void)
{
	int			iMsgResult = wxID_CANCEL;

	if(NULL != m_pclsTextCtrl_TextEdit)
	{
		if(wxEmptyString == m_strWorkingFileName)
		{
			wxFileDialog clsFileDlg(this, _I18N("Save file"), wxEmptyString, wxEmptyString, wxT("Text file(*.txt)|*.txt|All files|(*.*)"));

			iMsgResult = clsFileDlg.ShowModal();

			if(wxID_OK == iMsgResult)
			{
				m_strWorkingFileName = clsFileDlg.GetPath();
			}
		}
		else
		{
			iMsgResult = wxID_OK;
		}
		m_pclsTextCtrl_TextEdit->SaveFile(m_strWorkingFileName);

		m_pclsStatusBar_StatusBar->SetLabel(m_strWorkingFileName);
	}

	return iMsgResult;
}

